import axios from 'axios'
import md5 from 'js-md5'
// import { Toast } from 'vant'

const baseUrl = process.env.API_BASE_URL
// const service = axios.create({
//   baseURL: baseUrl, // api的base_url
//   timeout: 5000 // 请求超时时间
// })

// respone响应拦截器
// service.interceptors.response.use(response => {
//   const res = response.data
//   if (res.code !== 0) {
//     Toast.fail('网络异常')
//   } else {
//     return response.data
//   }
// })

function request (url, method, data) {
  return new Promise((resolve, reject) => {
    const v = '1.0.0'
    const t = Date.now()
    const key = 'lockapi.jijiakeji-co.com'
    const s = md5(v + '&' + t + '&' + key)
    const constant = {
      v,
      t,
      s
    }
    const obj = Object.assign({}, constant, data)
    axios({
      url: baseUrl + url,
      method,
      params: {
        ...obj
      }
    })
      .then(res => {
        if (res.data.code === 0) {
          resolve(res.data)
        }
      })
  })
}

function get (obj) {
  return request(obj.url, 'GET', obj.data)
}

export default {
  request,
  get
}
